/*
 * Created by JFormDesigner on Wed Oct 28 12:52:15 GMT+08:00 2020
 */

package file;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import file.constant.MenuItemEnum;
import file.constant.SysConstant;
import file.constant.ViewByEnum;
import file.listener.*;
import file.model.FileModel;
import file.untils.FileUntils;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 1
 */
public class FileFrame extends JFrame {

    // 默认头部树
    static DefaultMutableTreeNode topTree = new DefaultMutableTreeNode(SysConstant.DEFAULT_DRIVER_NAME);
    static volatile DefaultMutableTreeNode node = null;


    public static ImageIcon folderImage;
    static {
        folderImage = new ImageIcon(ClassLoader.getSystemResource("folderTree.png"));

    }

    public FileFrame() {
        initComponents();
    }

    public FileFrame(String name) {
        initComponents();
        // 初始化左子树
        initLeftTree();
        // 初始化个性化设置
        initMyStyle();
        addListener();
        super.setName(name);
    }

    /**
     * 为各组件添加监视器
     */
    private void addListener() {
        // 为左子树添加监视器
        leftTree.addMouseListener(new LeftTreeListener(this));
        filePanel.addMouseListener(new MenuListener(this));
        changeName.addMouseListener(new ChangeNameBtnListener(this));
        searchBtn.addMouseListener(new SearchBtnListener(this) );
        searchName.addFocusListener(new SearchNameFocus(this));
    }

    /**
     * 初始化自己风格
     */
    private void initMyStyle() {
        // 为leftTree改变图片
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)this.leftTree.getCellRenderer();
        cellRenderer.setLeafIcon(folderImage);
        this.getPathLable().setText("");
        // scrollPane1.setViewportView(leftTree);

        //scrollPane3.setViewportView(filePanel);

        // 添加菜单子项目
        addMenuItem();
        // 添加查看方式
        addView();

    }

    /**
     * 添加查看方式
     */
    private void addView() {
        ViewByEnum tiled = ViewByEnum.平铺;
        ViewByEnum smallPicture = ViewByEnum.小图标;
        ViewByEnum largerPicture = ViewByEnum.大图标;
        ViewByEnum detail = ViewByEnum.详细信息;
        List<ViewByEnum> ve = new ArrayList<>();
        ve.add(tiled);
        ve.add(smallPicture);
        ve.add(ViewByEnum.中图标);
        ve.add(largerPicture);
        ve.add(detail);
        ve.forEach(e -> {
            this.viewCombox.addItem(e);
            this.viewCombox.addItemListener(new ViewListener(this,e));
        });
    }

    /**
     * 添加菜单项
     */
    private void addMenuItem() {

        List<MenuItemEnum> mei =  new ArrayList<>();
        mei.add(MenuItemEnum.createFile);
        mei.add(MenuItemEnum.createFolder);
        mei.add(MenuItemEnum.copy);
        mei.add(MenuItemEnum.paste);
        mei.add(MenuItemEnum.back);
        mei.add(MenuItemEnum.remove);
        mei.add(MenuItemEnum.shear);
        mei.add(MenuItemEnum.reName);
        JMenuItem jMenuItem = null;
        for (MenuItemEnum menuItemEnum : mei) {
            jMenuItem = new JMenuItem();
            jMenuItem.setText(menuItemEnum.getName());
            jMenuItem.addMouseListener(new MenuItemListener(this,menuItemEnum) );
            menu.add(jMenuItem);
        }


    }


    private DefaultMutableTreeNode initLeftTree() {
        List<FileModel> rootModel = FileUntils.listDrive();
        if (rootModel != null) {
            this.getPathLable().setText(rootModel.get(0).getName());
            for (FileModel model : rootModel) {

               /* Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            node = new DefaultMutableTreeNode(model.getFullName());
                            FileUntils.listRootFile(model,node);
                            topTree.add(node);
                            // leftTree = new JTree (topTree);
                        }
                    } );
                    thread.start();*/
                System.out.println("更新一下 Jtree");
                model.setName(model.getFullName());
                node = new DefaultMutableTreeNode(model);
                FileUntils.listRootFile(model,node);
                topTree.add(node);
            }
            // leftTree = new LeftTree(topTree);
            return topTree;
        }
        return null;
    }

    private void leftTreeValueChanged(TreeSelectionEvent e) {
        // TODO add your code here
    }

    private void leftTreeMouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    public JDialog getDialog1() {
        return dialog1;
    }

    public void setDialog1(JDialog dialog1) {
        this.dialog1 = dialog1;
    }

    public JPanel getPanel13() {
        return panel13;
    }

    public void setPanel13(JPanel panel13) {
        this.panel13 = panel13;
    }

    public JPanel getPanel14() {
        return panel14;
    }

    public void setPanel14(JPanel panel14) {
        this.panel14 = panel14;
    }

    public JLabel getLabel8() {
        return label8;
    }

    public void setLabel8(JLabel label8) {
        this.label8 = label8;
    }

    public JScrollPane getScrollPane2() {
        return scrollPane2;
    }

    public void setScrollPane2(JScrollPane scrollPane2) {
        this.scrollPane2 = scrollPane2;
    }

    public JTextArea getChangeLable() {
        return changeLable;
    }

    public void setChangeLable(JTextArea changeLable) {
        this.changeLable = changeLable;
    }

    public JButton getChangeName() {
        return changeName;
    }

    public void setChangeName(JButton changeName) {
        this.changeName = changeName;
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
        panel3 = new JPanel();
        panel4 = new JPanel();
        center = new JPanel();
        top = new JPanel();
        topLeft = new JPanel();
        panel17 = new JPanel();
        pathLable = compFactory.createLabel("text");
        panel18 = new JPanel();
        panel21 = new JPanel();
        panel22 = new JPanel();
        panel23 = new JPanel();
        searchBtn = new JLabel();
        panel24 = new JPanel();
        searchName = new JTextArea();
        panel19 = new JPanel();
        label7 = new JLabel();
        viewCombox = new JComboBox();
        panel9 = new JPanel();
        mainBack = new JPanel();
        panel10 = new JPanel();
        panel11 = new JPanel();
        scrollPane1 = new JScrollPane();
        leftTree = new LeftTree(initLeftTree());
        panel5 = new JPanel();
        scrollPane3 = new JScrollPane();
        filePanel = new JPanel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        hiddleLable = new JLabel();
        panel12 = new JPanel();
        dialog1 = new JDialog();
        panel13 = new JPanel();
        panel14 = new JPanel();
        label8 = new JLabel();
        scrollPane2 = new JScrollPane();
        changeLable = new JTextArea();
        changeName = new JButton();
        menu = new JPopupMenu();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel3 ========
        {
            panel3.setLayout(new BorderLayout());
        }
        contentPane.add(panel3, BorderLayout.LINE_END);

        //======== panel4 ========
        {
            panel4.setLayout(new BorderLayout());
        }
        contentPane.add(panel4, BorderLayout.LINE_START);

        //======== center ========
        {
            center.setLayout(new BorderLayout());

            //======== top ========
            {
                top.setBackground(Color.white);
                top.setLayout(new FlowLayout(FlowLayout.LEFT));

                //======== topLeft ========
                {
                    topLeft.setBackground(Color.white);
                    topLeft.setLayout(new BorderLayout());

                    //======== panel17 ========
                    {
                        panel17.setBackground(Color.white);
                        panel17.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 17));

                        //---- pathLable ----
                        pathLable.setFont(pathLable.getFont().deriveFont(pathLable.getFont().getSize() + 14f));
                        pathLable.setForeground(new Color(102, 100, 100));
                        pathLable.setPreferredSize(new Dimension(300, 34));
                        panel17.add(pathLable);
                    }
                    topLeft.add(panel17, BorderLayout.WEST);

                    //======== panel18 ========
                    {
                        panel18.setBackground(Color.white);
                        panel18.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));

                        //======== panel21 ========
                        {
                            panel21.setBackground(new Color(238, 238, 238));
                            panel21.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

                            //======== panel22 ========
                            {
                                panel22.setLayout(new BorderLayout());

                                //======== panel23 ========
                                {
                                    panel23.setLayout(new FlowLayout());

                                    //---- searchBtn ----
                                    searchBtn.setIcon(new ImageIcon(getClass().getResource("/sousou.png")));
                                    panel23.add(searchBtn);
                                }
                                panel22.add(panel23, BorderLayout.WEST);

                                //======== panel24 ========
                                {
                                    panel24.setFocusable(false);
                                    panel24.setPreferredSize(new Dimension(120, 30));
                                    panel24.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));

                                    //---- searchName ----
                                    searchName.setBackground(new Color(238, 238, 238));
                                    searchName.setRows(1);
                                    searchName.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 14));
                                    searchName.setForeground(new Color(102, 100, 100));
                                    searchName.setText("\u641c\u7d20\u6211\u7684\u6587\u4ef6");
                                    panel24.add(searchName);
                                }
                                panel22.add(panel24, BorderLayout.CENTER);
                            }
                            panel21.add(panel22);
                        }
                        panel18.add(panel21);
                    }
                    topLeft.add(panel18, BorderLayout.CENTER);

                    //======== panel19 ========
                    {
                        panel19.setBackground(Color.white);
                        panel19.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 20));

                        //---- label7 ----
                        label7.setText("\u67e5\u770b\u65b9\u5f0f");
                        label7.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 14));
                        label7.setForeground(new Color(102, 100, 100));
                        panel19.add(label7);
                        panel19.add(viewCombox);
                    }
                    topLeft.add(panel19, BorderLayout.EAST);
                }
                top.add(topLeft);
            }
            center.add(top, BorderLayout.NORTH);

            //======== panel9 ========
            {
                panel9.setLayout(new FlowLayout(FlowLayout.RIGHT));
            }
            center.add(panel9, BorderLayout.EAST);

            //======== mainBack ========
            {
                mainBack.setLayout(new BorderLayout());

                //======== panel10 ========
                {
                    panel10.setLayout(new BorderLayout());

                    //======== panel11 ========
                    {
                        panel11.setLayout(new BorderLayout());

                        //======== scrollPane1 ========
                        {
                            scrollPane1.setViewportView(leftTree);
                        }
                        panel11.add(scrollPane1, BorderLayout.CENTER);
                    }
                    panel10.add(panel11, BorderLayout.LINE_START);

                    //======== panel5 ========
                    {
                        panel5.setLayout(new FlowLayout());
                    }
                    panel10.add(panel5, BorderLayout.PAGE_START);

                    //======== scrollPane3 ========
                    {
                        scrollPane3.setPreferredSize(new Dimension(100, 50));
                        scrollPane3.setFocusTraversalPolicyProvider(true);

                        //======== filePanel ========
                        {
                            filePanel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
                            filePanel.setForeground(new Color(102, 100, 100));
                            filePanel.setPreferredSize(new Dimension(600, 900));
                            filePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
                        }
                        scrollPane3.setViewportView(filePanel);
                    }
                    panel10.add(scrollPane3, BorderLayout.CENTER);
                }
                mainBack.add(panel10, BorderLayout.CENTER);
            }
            center.add(mainBack, BorderLayout.CENTER);
        }
        contentPane.add(center, BorderLayout.CENTER);

        //======== panel1 ========
        {
            panel1.setLayout(new BorderLayout());
        }
        contentPane.add(panel1, BorderLayout.PAGE_START);

        //======== panel2 ========
        {
            panel2.setLayout(new BorderLayout());

            //---- hiddleLable ----
            hiddleLable.setVisible(false);
            panel2.add(hiddleLable, BorderLayout.EAST);

            //======== panel12 ========
            {
                panel12.setLayout(new FlowLayout());
            }
            panel2.add(panel12, BorderLayout.CENTER);
        }
        contentPane.add(panel2, BorderLayout.PAGE_END);
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(new BorderLayout());

            //======== panel13 ========
            {
                panel13.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40));

                //======== panel14 ========
                {
                    panel14.setLayout(new GridBagLayout());
                    ((GridBagLayout)panel14.getLayout()).columnWidths = new int[] {0, 0};
                    ((GridBagLayout)panel14.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
                    ((GridBagLayout)panel14.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
                    ((GridBagLayout)panel14.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};

                    //---- label8 ----
                    label8.setText("\u8bf7\u8f93\u5165\u65b0\u7684\u6587\u4ef6\u540d\u79f0");
                    panel14.add(label8, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 0), 0, 0));

                    //======== scrollPane2 ========
                    {
                        scrollPane2.setViewportView(changeLable);
                    }
                    panel14.add(scrollPane2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 5, 0), 0, 0));

                    //---- changeName ----
                    changeName.setText("\u786e\u5b9a");
                    panel14.add(changeName, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
                }
                panel13.add(panel14);
            }
            dialog1ContentPane.add(panel13, BorderLayout.CENTER);
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    /**
     * 设置Jtree样式
     * @param leftTree
     */
    private void setLeftTreeStyle(JTree leftTree) {
        DefaultTreeCellRenderer defaultTreeCellRenderer = (DefaultTreeCellRenderer)leftTree.getCellRenderer();
        defaultTreeCellRenderer.setLeafIcon(new ImageIcon(ClassLoader.getSystemResource(SysConstant.IMAGE_FOLDER_SAMALL)));
    }

    public JTree getLeftTree() {
        return leftTree;
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel3;
    private JPanel panel4;
    private JPanel center;
    private JPanel top;
    private JPanel topLeft;
    private JPanel panel17;
    private JLabel pathLable;
    private JPanel panel18;
    private JPanel panel21;
    private JPanel panel22;
    private JPanel panel23;
    private JLabel searchBtn;
    private JPanel panel24;
    private JTextArea searchName;
    private JPanel panel19;
    private JLabel label7;
    private JComboBox viewCombox;
    private JPanel panel9;
    private JPanel mainBack;
    private JPanel panel10;
    private JPanel panel11;
    private JScrollPane scrollPane1;
    private LeftTree leftTree;
    private JPanel panel5;
    private JScrollPane scrollPane3;
    private JPanel filePanel;
    private JPanel panel1;
    private JPanel panel2;
    private JLabel hiddleLable;
    private JPanel panel12;
    private JDialog dialog1;
    private JPanel panel13;
    private JPanel panel14;
    private JLabel label8;
    private JScrollPane scrollPane2;
    private JTextArea changeLable;
    private JButton changeName;
    private JPopupMenu menu;
    // JFormDesigner - End of variables declaration  //GEN-END:variables




    public JPanel getPanel3() {
        return panel3;
    }

    public void setPanel3(JPanel panel3) {
        this.panel3 = panel3;
    }

    public JPanel getPanel4() {
        return panel4;
    }

    public void setPanel4(JPanel panel4) {
        this.panel4 = panel4;
    }

    public JPanel getCenter() {
        return center;
    }

    public void setCenter(JPanel center) {
        this.center = center;
    }

    public JPanel getTop() {
        return top;
    }

    public void setTop(JPanel top) {
        this.top = top;
    }

    public JPanel getTopLeft() {
        return topLeft;
    }

    public void setTopLeft(JPanel topLeft) {
        this.topLeft = topLeft;
    }

    public JLabel getPathLable() {
        return pathLable;
    }

    public void setPathLable(JLabel pathLable) {
        this.pathLable = pathLable;
    }




    public JPanel getPanel9() {
        return panel9;
    }

    public void setPanel9(JPanel panel9) {
        this.panel9 = panel9;
    }



    public JPanel getMainBack() {
        return mainBack;
    }

    public void setMainBack(JPanel mainBack) {
        this.mainBack = mainBack;
    }



    public JPanel getPanel10() {
        return panel10;
    }

    public void setPanel10(JPanel panel10) {
        this.panel10 = panel10;
    }

    public JPanel getPanel11() {
        return panel11;
    }

    public void setPanel11(JPanel panel11) {
        this.panel11 = panel11;
    }

    public JScrollPane getScrollPane1() {
        return scrollPane1;
    }

    public void setScrollPane1(JScrollPane scrollPane1) {
        this.scrollPane1 = scrollPane1;
    }

    public JPanel getFilePanel() {
        return filePanel;
    }

    public void setFilePanel(JPanel filePanel) {
        this.filePanel = filePanel;
    }


    public JPanel getPanel1() {
        return panel1;
    }

    public void setPanel1(JPanel panel1) {
        this.panel1 = panel1;
    }

    public JPanel getPanel2() {
        return panel2;
    }

    public void setPanel2(JPanel panel2) {
        this.panel2 = panel2;
    }

    public static DefaultMutableTreeNode getTopTree() {
        return topTree;
    }

    public static void setTopTree(DefaultMutableTreeNode topTree) {
        FileFrame.topTree = topTree;
    }

    public static DefaultMutableTreeNode getNode() {
        return node;
    }

    public static void setNode(DefaultMutableTreeNode node) {
        FileFrame.node = node;
    }

    public static ImageIcon getFolderImage() {
        return folderImage;
    }

    public static void setFolderImage(ImageIcon folderImage) {
        FileFrame.folderImage = folderImage;
    }




    public JPanel getPanel12() {
        return panel12;
    }

    public void setPanel12(JPanel panel12) {
        this.panel12 = panel12;
    }

    public JPopupMenu getMenu() {
        return menu;
    }

    public void setMenu(JPopupMenu menu) {
        this.menu = menu;
    }


    public JLabel getHiddleLable() {
        return hiddleLable;
    }

    public void setHiddleLable(JLabel hiddleLable) {
        this.hiddleLable = hiddleLable;
    }

    public JPanel getPanel17() {
        return panel17;
    }

    public void setPanel17(JPanel panel17) {
        this.panel17 = panel17;
    }

    public JPanel getPanel18() {
        return panel18;
    }

    public void setPanel18(JPanel panel18) {
        this.panel18 = panel18;
    }

    public JPanel getPanel21() {
        return panel21;
    }

    public void setPanel21(JPanel panel21) {
        this.panel21 = panel21;
    }

    public JPanel getPanel22() {
        return panel22;
    }

    public void setPanel22(JPanel panel22) {
        this.panel22 = panel22;
    }

    public JPanel getPanel23() {
        return panel23;
    }

    public void setPanel23(JPanel panel23) {
        this.panel23 = panel23;
    }



    public JPanel getPanel24() {
        return panel24;
    }

    public void setPanel24(JPanel panel24) {
        this.panel24 = panel24;
    }

    public JTextArea getSearchName() {
        return searchName;
    }

    public void setSearchName(JTextArea searchName) {
        this.searchName = searchName;
    }

    public JPanel getPanel19() {
        return panel19;
    }

    public void setPanel19(JPanel panel19) {
        this.panel19 = panel19;
    }

    public JLabel getLabel7() {
        return label7;
    }

    public void setLabel7(JLabel label7) {
        this.label7 = label7;
    }

    public JLabel getSearchBtn() {
        return searchBtn;
    }

    public void setSearchBtn(JLabel searchBtn) {
        this.searchBtn = searchBtn;
    }

    public JComboBox getViewCombox() {
        return viewCombox;
    }

    public void setViewCombox(JComboBox viewCombox) {
        this.viewCombox = viewCombox;
    }



    private class LeftTree extends JTree {
        private LeftTree(DefaultMutableTreeNode topTree) {
            super(topTree);
            initComponents();
        }


        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        // JFormDesigner - End of variables declaration  //GEN-END:variables
    }

    public JPanel getPanel5() {
        return panel5;
    }

    public void setPanel5(JPanel panel5) {
        this.panel5 = panel5;
    }

    public JScrollPane getScrollPane3() {
        return scrollPane3;
    }

    public void setScrollPane3(JScrollPane scrollPane3) {
        this.scrollPane3 = scrollPane3;
    }
}
